package 算法.leetcode.linked.list;

import org.junit.Test;

/**
 * @author lchenglong
 * @date 2020/12/24
 */
public class HasCycle extends LinkListBase{
    @Test
    public void test() {
        System.out.println(hasCycle(create()));
    }

    public boolean hasCycle(ListNode head) {
        if (head == null || head.next==null)
            return false;
        ListNode fast = head.next;
        ListNode slow = head;
        while (fast!=slow) {
            if (fast.next == null || fast.next.next == null)
                return false;
            fast = fast.next.next;
            slow = slow.next;
        }
        return true;
    }

    ListNode create() {
        ListNode head = new ListNode(3);
        ListNode l1 = new ListNode(5);
        head.next = l1;
        ListNode l2 = new ListNode(7);
        l1.next = l2;
        l2.next = null;
        return head;
    }
}
